﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dotnet.Utils.Utility.Other
{
    /// <summary>
    /// 版本帮助实体
    /// </summary>
    public static class VersionHelper
    {
        /// <summary>
        /// 修正版本号- v1.0.1修正为1.0.1
        /// </summary>
        /// <param name="version">版本号</param>
        /// <returns></returns>
        public static string ReviseVersion(string version)
        {
            if (string.IsNullOrEmpty(version))
            {
                return string.Empty;
            }

            var result = version;
            if (version.StartsWith("v", StringComparison.OrdinalIgnoreCase))
            {
                result = version.Substring(1, version.Length - 1);
            }
            return result;
        }

        /// <summary>
        /// 检查版本号是否正确
        /// </summary>
        /// <param name="version">版本号</param>
        /// <returns></returns>
        public static bool IsCorrectVersion(string version)
        {
            return Version.TryParse(version, out var v);
        }

        /// <summary>
        /// 比较版本号大小
        /// 该方法被打包被打包构建策略引用, 请避免修改命名
        /// </summary>
        /// <param name="sourceVersion"></param>
        /// <param name="targetVersion"></param>
        /// <returns></returns>
        public static int CompareVersion(string sourceVersion, string targetVersion)
        {
            Version srcVer = new Version(sourceVersion);
            Version tarVer = new Version(targetVersion);
            return srcVer.CompareTo(tarVer);
        }

        /// <summary>
        /// 对版本号进行排序
        /// </summary>
        /// <param name="versions"></param>
        /// <returns></returns>
        public static List<string> SortVersion(List<string> versions, SortOrder order = SortOrder.Asc)
        {
            List<Version> sortedVers = new List<Version>();
            versions.ForEach(ver => sortedVers.Add(new Version(ver)));
            sortedVers.Sort();
            if (order != SortOrder.Asc)
            {
                sortedVers.Reverse();
            }

            return sortedVers.Select(x => x.ToString()).ToList();
        }

        /// <summary>
        /// 获取最新版本
        /// </summary>
        /// <param name="versions"></param>
        /// <returns></returns>
        public static string GetLatestVersion(List<string> versions)
        {
            if (versions == null || versions.Count == 0)
            {
                return string.Empty;
            }
            var ves = versions.Where(p => string.IsNullOrWhiteSpace(p) == false).ToList();
            List<string> sortedVers = SortVersion(ves);
            return sortedVers.Last();
        }

        /// <summary>
        /// 将简略版本号转换为全版本号
        /// 全版本格式为：00000001.00000000.00000000.00000000
        /// 简略版本格式为：1.0.0.0
        /// </summary>
        /// <param name="currentVersion">需要转换的简略版本号</param>
        /// <returns>返回转换好的全版本号</returns>
        public static string ConverSimpleToFullVersion(string currentVersion)
        {
            if (string.IsNullOrWhiteSpace(currentVersion))
            {
                return "";
            }
            Version v1 = new Version(currentVersion);
            StringBuilder result = new StringBuilder();
            result.Append(string.Format("{0:00000000}", v1.Major));
            result.Append(".");
            result.Append(string.Format("{0:00000000}", v1.Minor));
            result.Append(".");
            result.Append(string.Format("{0:00000000}", v1.Build));
            result.Append(".");
            result.Append(string.Format("{0:00000000}", v1.Revision));
            return result.ToString();
        }
    }

    /// <summary>
    /// 排序
    /// </summary>
    public enum SortOrder
    {
        /// <summary>
        /// 正序
        /// </summary>
        Asc,

        /// <summary>
        /// 倒序
        /// </summary>
        Desc
    }
}